package com.hzjiteng.newsbymvp.news.widget;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hzjiteng.newsbymvp.R;
import com.hzjiteng.newsbymvp.bean.NewsBean;
import com.hzjiteng.newsbymvp.comment.URLS;
import com.hzjiteng.newsbymvp.news.NewsListAdapter;
import com.hzjiteng.newsbymvp.news.presenter.NewsPrestener;
import com.hzjiteng.newsbymvp.news.presenter.NewsPrestenerImpl;
import com.hzjiteng.newsbymvp.news.views.NewsView;

import java.util.ArrayList;
import java.util.List;

import static android.support.v7.widget.RecyclerView.OnScrollListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsListFragment extends Fragment implements NewsView, SwipeRefreshLayout.OnRefreshListener, NewsListAdapter.OnItemClickListener {

    private static final String TAG = "NewsListFragment";

    public static NewsListFragment getInstance(int type) {
        NewsListFragment fragment = new NewsListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    private int type = NewsFragment.NEWS_TYPE_TOP;
    private int pageIndex = 0;
    private NewsPrestener newsPrestener;

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;

    private NewsListAdapter adapter;


    private List<NewsBean> list;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        type = bundle.getInt("type");
        newsPrestener = new NewsPrestenerImpl(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_list, container, false);
        initView(view);
        onRefresh();
        return view;
    }

    private void initView(View view) {
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeColors(R.color.primary,
                R.color.primary_dark, R.color.primary_light, R.color.accent);
        swipeRefreshLayout.setOnRefreshListener(this);


        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addOnScrollListener(listener);
        adapter = new NewsListAdapter(getActivity());
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
    }


    private RecyclerView.OnScrollListener listener = new OnScrollListener() {
        private int lastVisibleItem;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE
                    && lastVisibleItem + 1 == adapter.getItemCount()
                    && adapter.isShowFooter()) {
                Log.e(TAG, "加载更多...");
                newsPrestener.loadNews(type, pageIndex + URLS.PAZE_SIZE);
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    };


    @Override
    public void showProgress() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void addNews(List<NewsBean> list) {
        adapter.setShowFooter(true);
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        this.list.addAll(list);

        if (pageIndex == 0)
            adapter.setList(this.list);
        else {
            if (list == null || list.size() == 0)
                adapter.setShowFooter(false);
            adapter.notifyDataSetChanged();
        }
        pageIndex += URLS.PAZE_SIZE;
    }

    @Override
    public void hideProgress() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showLoadFailMsg(String msg) {
        Snackbar.make(recyclerView, msg, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onRefresh() {
        pageIndex = 0;
        if (list != null) {
            list.clear();
        }
        newsPrestener.loadNews(type, pageIndex);
    }

    @Override
    public void onItemClick(View view, int position) {
        NewsBean bean = adapter.getItemBean(position);
        Intent intent = new Intent(getActivity(), NewsDetilsActivity.class);
        intent.putExtra("news", bean);

        View transitionView = view.findViewById(R.id.ivNews);

        ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), transitionView, getString(R.string.transition_news_image));

        ActivityCompat.startActivity(getActivity(), intent, compat.toBundle());
    }
}
